﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using ApplicationLibrary;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using ApplicationLibrary.DBManager;

namespace Vintner.Database
{
    #region Radar Items
    internal class RadarItemsForm : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarItemsForm(SqlABConnection sqlconn, GlobalInfo gI)
            : base(sqlconn, gI)
        {
            rdrCodeColumn = VI_Items.Item.Name;
            rdrDescColumn = VI_Items.Description.Name;
            rdrNameSpace = "Vintner.Vintner.Forms.Items";
        }

        protected override Table GetRadarTable()
        {
            return new VI_Items();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(VI_Items.Name);
            qb.AddFrom(VI_Items.Name);
            qb.AddCompare(VI_Items.Item, "@p1");

            p1 = new SqlABParameter(base.ProviderType, "@p1", SqlDbType.NVarChar, 21);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(string code)
        {
            p1.Value = code;
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[VI_Items.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd)
        {
            p2 = new SqlABParameter(base.ProviderType, "@p2", SqlDbType.NVarChar, 1);
            qb.Clear();
            qb.AddSelectAll(VI_Items.Name);
            qb.AddFrom(VI_Items.Name);
            qb.AddCompare(VI_Items.Disabled, "@p2");

            sqlCmd.Parameters.Add(p2);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = "0";
        }
    }
    #endregion
}
